package 课上练习0419_scala_collection

object Scala_02Tuple extends App {
  println("===============创建元组====================")
  val t1 = (1, "bing", 20, 100) //四元组
  val t2, (a1, a2, a3, a4) = (1, "bing", 20, 100)
  val t3 = new Tuple4[Int, String, Int, Int](1, "bing", 20, 100)

  println("===============元素访问====================")
  println(t1.productElement(0)) // 下标访问
  println(t1._1) // _值访问

  println("===============遍历元组====================")
  for (elem <- t1.productIterator) println(elem)
  t1.productIterator.foreach(println)

  println("===============元组拉链====================")
  val arr1 = Array("hadoop", "spark", "flink")
  val arr2 = Array(1, 2)
  println(arr1.zip(arr2).mkString("Array(", ", ", ")")) // (hadoop,1), (spark,2)
  println(arr1.zipAll(arr2, "space", -1).mkString("Array(", ", ", ")")) // (hadoop,1), (spark,2), (flink,-1)
  println("带上数组下标的拉链: " + arr1.zipWithIndex.mkString("Array(", ", ", ")"))// (hadoop,0), (spark,1), (flink,2)
//  println("指定开始的数组下标的拉链: " + arr1.zip(Stream from 2).mkString("Array(", ", ", ")"))// deprecated
  println("指定开始的数组下标的拉链: " + arr1.zip(LazyList from 2).mkString("Array(", ", ", ")"))// (hadoop,0), (spark,1), (flink,2)

  println("===============元组解拉链====================")
  val t4 = arr1.zip(arr2)
  println(t4.mkString("Array(", ", ", ")"))
  println("1) " + t4.map(_._1).mkString("Array(", ", ", ")"))
  println("2) " + t4.map(_._2).mkString("Array(", ", ", ")"))
}